/*
 * Copyright (c) 2024. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.neusoft.tyut.hrs.mapper.controller;

import com.auth0.jwt.interfaces.Claim;
import com.neusoft.tyut.hrs.comon.CommonResult;
import com.neusoft.tyut.hrs.comon.ResultCode;
import com.neusoft.tyut.hrs.comon.util.JwtUtil;
import com.neusoft.tyut.hrs.service.EmpService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.neusoft.tyut.hrs.comon.controller.BasicController;

import java.util.Map;

@RestController // @Controller + @ResponseBody
@CrossOrigin("*")
@Slf4j
@RequestMapping("emp")
public class EmpController extends BasicController {
    @Autowired private EmpService empService;

    @GetMapping("/test")
    public CommonResult test() {
        return null;
    }
    /** get Emp by id */
    @GetMapping("/login/{logid}/{logpwd}")
    public CommonResult login(
            @PathVariable("logid") String logid,
            @PathVariable("logpwd") String logpwd,
            HttpServletRequest request) {
        log.info("login: logid={}, logpwd={}", logid, logpwd);
        String token = request.getHeader("Authoriztion");
        log.info("token: {}", token);

        return empService.login(logid, logpwd);
    }
    @GetMapping("/test/token/{token}")
    public CommonResult testToken(
            @PathVariable("token") String token
            ) {
        try {
            Map<String, Claim> stringClaimMap = JwtUtil.verifyToken(token);
        } catch (Exception e) {
            return   CommonResult.fail(ResultCode.TOKEN_INVALID);
        }


        return  CommonResult.success("ok");
    }
}
